//
//  XQLoginCodeView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQLoginCodeView.h"

@interface XQLoginCodeView()
@property (nonatomic,strong) UILabel *codeLb;
@property (nonatomic,strong) UIView *codeView;
@property (nonatomic,strong) UITextField *codeTextField;
@end

@implementation XQLoginCodeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.codeLb];
    [self addSubview:self.codeView];
    [self.codeView addSubview:self.codeTextField];
    [self.codeView addSubview:self.sendBtn];
}

- (void)makeConstraints {
    [self.codeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@18);
        make.height.mas_equalTo(28);
    }];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeLb.mas_bottom).offset(10);
        make.left.equalTo(@17);
        make.right.equalTo(@-28);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(@0);
    }];
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerY;
        make.right.equalTo(@-10);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(30);
    }];
    [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.bottom.equalTo(@0);
        make.right.equalTo(self.sendBtn.mas_left);
    }];
}

#pragma mark - public

- (void)clear {
    self.codeTextField.text = @"";

    [self shakeTextField];
}

- (void)becomeFirstResponder {
    [self.codeTextField becomeFirstResponder];
}


#pragma mark - private

// 监听文本变化的方法
- (void)textFieldDidChange:(UITextField *)textField {
    if (self.textFielHandle) {
        self.textFielHandle(textField.text);
    }
}

- (void)sendClick {
    if (self.sendHandle) {
        self.sendHandle();
    }
}

- (void)shakeTextField {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration = 0.05; // 动画持续时间
    animation.repeatCount = 2; // 重复次数
    animation.autoreverses = YES; // 动画反向
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.codeView.center.x - 10, self.codeView.center.y)];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.codeView.center.x + 10, self.codeView.center.y)];
    
    [self.codeView.layer addAnimation:animation forKey:@"position"];
}

#pragma mark - lazy

- (UILabel *)codeLb {
    if (!_codeLb) {
        _codeLb = [[UILabel alloc] init];
        _codeLb.text = @"Verifcation code";
        _codeLb.textColor = kMainTextColor;
        _codeLb.font = [UIFont systemFontOfSize:20];
    }
    return _codeLb;
}

- (UIView *)codeView {
    if (!_codeView) {
        _codeView = [[UIView alloc] init];
        _codeView.backgroundColor = [UIColor whiteColor];
        _codeView.layer.cornerRadius = 15;
    }
    return _codeView;
}

- (XQBaseSendMessageButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [XQBaseSendMessageButton buttonWithType:UIButtonTypeCustom];
        [_sendBtn setTitle:@"Get code" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
        _sendBtn.backgroundColor = kOrangeColor;
        _sendBtn.layer.cornerRadius = 15;
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_sendBtn addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc] init];
        _codeTextField.font = [UIFont boldSystemFontOfSize:20];
        _codeTextField.textAlignment = NSTextAlignmentLeft;
        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        _codeTextField.textColor = kBlackTextColor;
        _codeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Enter verification code" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#999999"]}];
        [_codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _codeTextField;
}


@end
